//
//  ReadQuestionTVC.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/28.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadQuestionTVC.h"

@implementation ReadQuestionTVC

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
//        self.contentView.backgroundColor = [UIColor colorWithRed:247/255.0 green:248/255.0 blue:249/255.0 alpha:1];
        
        self.titleLabel = [YHYBaseLabel new];
        self.titleLabel.font = [UIFont systemFontOfSize:20];
//        self.titleLabel.textColor = [UIColor blueColor];
        self.detailLabel = [YHYBaseLabel new];
        self.smallView = [YHYBaseView new];
        
        self.titleLabel.numberOfLines = 0;
        self.detailLabel.numberOfLines = 0;
        
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_detailLabel];
        [self.contentView addSubview:_smallView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    _titleHeight = [[self class]heightWithTitle:_titleLabel.text];
    [_titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.width.equalTo(@(INWIDTH - 20));
        make.height.equalTo(@(_titleHeight));
        
    }];
    
    _detailHeight = [[self class]textWithHeight:_detailLabel.text];
    [_detailLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_titleLabel.mas_bottom).offset(10);
        make.left.equalTo(self.contentView).offset(10);
        make.width.equalTo(@(INWIDTH - 20));
        make.height.equalTo(@(_detailHeight));
        
    }];
    
    [_smallView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.contentView).offset(-1);
        make.width.equalTo(@(INWIDTH - 20));
        make.height.equalTo(@1);
        
    }];
}

+ (CGFloat)textWithHeight:(NSString *)text {
    if (text == nil) {
        return 0;
    }
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey:NSFontAttributeName];
    CGSize size = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil].size;
    
    return size.height + 20;

}

+ (CGFloat)heightWithTitle:(NSString *)text {
    if (text == nil) {
        return 0;
    }
    
    NSDictionary *fontDic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:20] forKey:NSFontAttributeName];
    CGSize size = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 10000) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:fontDic context:nil].size;
    
    return size.height;
    
}

- (void)setQuestionModel:(DetailQuestionDataModel *)questionModel {
    if (_questionModel != questionModel) {
        [_questionModel release];
       _questionModel = [questionModel retain];
    }
    
    _titleLabel.text = questionModel.question_title;
    _detailLabel.text = questionModel.question_content;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
